package com.hengtiansoft.lfy.utils;

import android.content.Context;

import com.iflytek.cloud.EvaluatorListener;
import com.iflytek.cloud.SpeechConstant;
import com.iflytek.cloud.SpeechEvaluator;

/**
 * Created by suyiyang on 2016/9/26.
 */

public class XFEvaluatorManager {

    //前端点
    private String VAD_BOS = "5000";
    //后端点
    private String VAD_EOS = "2000";
    //语音测评
    private SpeechEvaluator mSpeechEvaluator;
    private static XFEvaluatorManager manager;

    private XFEvaluatorManager(){}
    //单例模式
    public static XFEvaluatorManager getKdxfInstance(){
        if(manager == null){
            manager = new XFEvaluatorManager();
        }
        return manager;
    }

    /**
     * 科大讯飞语音测评
     * @param mContext
     */
    public void initEvaluator(Context mContext){
        // 创建评测对象
        mSpeechEvaluator = SpeechEvaluator.createEvaluator(
                mContext, null);
        // 设置评测语种en_us(英语)、zh_cn(汉语)
        mSpeechEvaluator.setParameter(SpeechConstant.LANGUAGE, "en_us");
        // 设置评测题型 可选值:read_syllable(单字,汉语专有)、read_word(词 语)、read_sentence(句子)
        mSpeechEvaluator.setParameter(SpeechConstant.ISE_CATEGORY, "read_sentence");
        // 设置试题编码类型
        mSpeechEvaluator.setParameter(SpeechConstant.TEXT_ENCODING, "utf-8");
        // 设置前、后端点超时
        mSpeechEvaluator.setParameter(SpeechConstant.VAD_BOS, VAD_BOS);
        mSpeechEvaluator.setParameter(SpeechConstant.VAD_EOS, VAD_EOS);
        // 设置录音超时,设置成-1 则无超时限制
        mSpeechEvaluator.setParameter(SpeechConstant.KEY_SPEECH_TIMEOUT, "-1");
        // 设置结果等级,不同等级对应不同的详细程度 将 result_level 设置成 plain 将返回精简结果(注:汉语评测返回 的都是详细结果)complete
        mSpeechEvaluator.setParameter(SpeechConstant.RESULT_LEVEL, "complete");

        mSpeechEvaluator.setParameter(SpeechConstant.AUDIO_FORMAT, "wav");

//        //朗读跟踪 { null, "enable", "disable" }
//        mSpeechEvaluator.setParameter(SpeechConstant.ISE_AUTO_TRACKING, "enable");
//        //{ null, "easy", "hard" }
//        mSpeechEvaluator.setParameter(SpeechConstant.ISE_TRACK_TYPE, "easy");

    }
    /**
     * 开始语音测评
     * @param evaText 需要测评的文本
     * @param mEvaluatorListener
     * @param voicePath 保存语音的路径及名称 Environment.getExternalStorageDirectory()+"/lfy/"+ voiceName +".wav"
     * 注：限制180字节 否则报1019错误
     */
    public int  startEvaluator(String evaText, EvaluatorListener mEvaluatorListener, String voicePath){
        mSpeechEvaluator.setParameter(SpeechConstant.ISE_AUDIO_PATH,voicePath);
        return   mSpeechEvaluator.startEvaluating(evaText, null, mEvaluatorListener);
    }

    /**
     * 停止语音测评
     */
    public void  stopEvaluating(){
        if(mSpeechEvaluator != null)
            mSpeechEvaluator.stopEvaluating();
    }
    /**
     * 取消语音测评
     */
    public void  cancelEvaluating(){
        if(mSpeechEvaluator != null)
            mSpeechEvaluator.cancel();
    }

}
